package com.yaozheng.vocationaltraining.utils;

import android.widget.ImageView;

import com.bumptech.glide.DrawableTypeRequest;
import com.bumptech.glide.RequestManager;
import com.yaozheng.vocationaltraining.widget.CircleImageView;

public class BitmapLoader {
    private int resource;

    private RequestManager requestManager;

    /**
     */
    public BitmapLoader(RequestManager requestManager) {
        this(requestManager, 0);
    }

    /**
     */
    public BitmapLoader(RequestManager requestManager, int resource) {
        this.requestManager = requestManager;
        this.resource = resource;
    }

    public void setDefaultResource(int resource) {
        if (this.resource != resource) {
            this.resource = resource;

        }
    }

    public void displayImage(String url, ImageView imageView, int resource) {
        if (url == null || "".equals(url) || imageView == null) {
            if (resource > 0) {
                imageView.setImageResource(resource);
            }
            return;
        }
        int width = imageView.getWidth();
        int height = imageView.getHeight();
        DrawableTypeRequest<String> drawableTypeRequest = requestManager
                .load(url);
        if (imageView instanceof CircleImageView) {
            drawableTypeRequest.dontAnimate();
        } else {
            drawableTypeRequest.crossFade(0);
        }
        if (resource > 0) {
            drawableTypeRequest.placeholder(resource)
                    .error(resource);
        } else {
        }
        if (width <= 0 && height <= 0) {
            if (imageView.getLayoutParams() != null) {
                width = imageView.getLayoutParams().width;
                height = imageView.getLayoutParams().height;
            }
        }
        if (width > 0 && height > 0) {
            drawableTypeRequest.override(width, height);
        }
        drawableTypeRequest.into(imageView);
    }

    public void displayImage(String url, ImageView imageView) {
//        System.out.println("url------" + url);
        displayImage(url, imageView, resource);
    }
}
